const _ = require("lodash");
//01lodash中的纯函数 _.memoize
function getArea(r) {
  console.log(r);
  return Math.PI * r * r;
}
// 只会打印一次半径 后续调用结果会被缓存
// let getAreaWithMemory=_.memoize(getArea)
// console.log(getAreaWithMemory(4))
// console.log(getAreaWithMemory(4))
// console.log(getAreaWithMemory(4))
//02手写memoize方法
function memoize(f) {
  let cache = {};
  return function () {
    let k = JSON.stringify(arguments);
    cache[k] = cache[k] || f.apply(f, arguments);
    return cache[k];
  };
}
let getAreaWithMemory = memoize(getArea);
console.log(getAreaWithMemory(4));
console.log(getAreaWithMemory(4));
console.log(getAreaWithMemory(4));
